#ifndef _H_C_NEW_BACKEND_MSG_H_
#define _H_C_NEW_BACKEND_MSG_H_

#include "IServiceMsg.h"
#include "ServiceDelegate.h"
#include "IService.h"
#include "CServiceBackendHelper.h"

namespace QWER
{
	template<typename T>
	QDLL_EXPORT QCLASS CNewBackendMsg: public IServiceMsg
	{
	public:
		CNewBackendMsg(IRunner& roRunner, IService& roService, const DNewServiceBackend<T>& cbNewBackend):
			m_roRunner(roRunner), m_roService(roService), m_cbNewBackend(cbNewBackend)
		{
		}

		virtual EServiceMsgType GetMsgType() QCONST_FUNC QOVERRIDE
		{
			return EServiceMsgType::NewBackendMsg;
		}

	protected:
		virtual CPromise<BOOLN>* _OnRecv() QOVERRIDE
		{
			m_roService._SetBackend(QMOVE(std::unique_ptr<T>(m_cbNewBackend(&m_roService).release())));
			m_roService.m_poBackend->_ServiceBackendHelper()->_SetRunner(&m_roRunner);
			m_roService.m_poBackend->_ExecAwake();
            return NULL_PTR;
		}

	private:
		IRunner& m_roRunner;
		IService& m_roService;
		DNewServiceBackend<T> m_cbNewBackend;
	};
}

#endif
